package com.isoft.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.entity.BeginTime;
import com.isoft.entity.Endplace;
import com.isoft.entity.FlightLine;
import org.apache.ibatis.annotations.*;


import java.util.List;
import java.util.Objects;

@Mapper
public interface FlightLineDao extends BaseMapper<FlightLine> {
    @Select("SELECT * FROM AIRPORT.FLIGHT where ISPERMIT = 1 and ISDELETE=0")
    public List<FlightLine> selectAllLine();
    //分页
    @Select("SELECT * FROM AIRPORT.FLIGHT WHERE ISPERMIT = 1 AND ISDELETE = 0")
    IPage<FlightLine> selectPage(Page<?> page);
    //未审核航班
    @Select("SELECT * FROM AIRPORT.FLIGHT where ISPERMIT = 0")
    public List<FlightLine> unPublicLine();
    @Select("SELECT * FROM AIRPORT.FLIGHT WHERE ISPERMIT = 0")
    IPage<FlightLine> selectUnPublicPage(Page<?> page);

    @Insert("INSERT into AIRPORT.FLIGHT(UNIQUENUM,FLIGHTNUM,BEGINTIME,ENDTIME,JICHANG,ENDPLACE,ENGENDPLACE,APSN,BEGINPLACE,ENGBEGPLACE,ISPERMIT) " +
            "VALUES(#{uniquenum},#{flightnum},#{begintime},#{endtime}," +
            "#{jichang},#{endplace},#{engendplace},#{apsn},#{beginplace},#{engbegplace},#{ispermit})")
    public int addLine(FlightLine flightLine);

    @Update("<script>" +
            "update AIRPORT.FLIGHT" +
            "        <set> " +
            "            <if test=\"null != uniquenum\"> " +
            "               AIRPORT.FLIGHT.UNIQUENUM =#{uniquenum},                 " +
            "            </if> " +
            "            <if test=\"null != flightnum\"> " +
            "               AIRPORT.FLIGHT.FLIGHTNUM =#{flightnum},                 " +
            "            </if> " +
            "            <if test=\"null != begintime\"> " +
            "               AIRPORT.FLIGHT.BEGINTIME =#{begintime},                 " +
            "            </if> " +
            "            <if test=\"null != endtime\"> " +
            "               AIRPORT.FLIGHT.ENDTIME =#{endtime},                 " +
            "            </if> " +
            "            <if test=\"null != jichang\"> " +
            "               AIRPORT.FLIGHT.JICHANG =#{jichang},                 " +
            "            </if> " +
            "            <if test=\"null != endplace\"> " +
            "               AIRPORT.FLIGHT.ENDPLACE =#{endplace},                 " +
            "            </if> " +
            "            <if test=\"null !=beginplace\"> " +
            "                AIRPORT.FLIGHT.BEGINPLACE =#{beginplace},  " +
            "            </if> " +
            "            <if test=\"null !=engbegplace\"> " +
            "                AIRPORT.FLIGHT.ENGBEGPLACE =#{engbegplace},  " +
            "            </if> " +
            "            <if test=\"null != engendplace\"> " +
            "                AIRPORT.FLIGHT.ENGENDPLACE =#{engendplace},                  " +
            "            </if> " +
            "            <if test=\"null !=apsn\"> " +
            "                AIRPORT.FLIGHT.APSN =#{apsn},  " +
            "            </if> " +
            "        </set> " +
            "        where AIRPORT.FLIGHT.ID=#{id}"
            +"</script>")
    public int updateLine(FlightLine flightLine);

    @Select("SELECT * FROM AIRPORT.FLIGHT WHERE BEGINTIME LIKE CONCAT('%', #{day}, '%');")
    public List<FlightLine> selectByDay(String day);
    @Update("UPDATE AIRPORT.FLIGHT SET ISDELETE = 1 WHERE ID=#{id}")
    public int deleteLine(Integer id);

    @Update("update AIRPORT.FLIGHT SET ISPERMIT=1 WHERE ID = #{ID};")
    public int changePermit(Integer ID);

    @Select("SELECT * FROM AIRPORT.FLIGHT WHERE BEGINTIME LIKE CONCAT('%', #{starttime}, '%') AND ENDTIME LIKE CONCAT('%', #{endtime}, '%') AND ENDPLACE LIKE CONCAT('%', #{city}, '%') AND ISPERMIT=1 and ISDELETE=0")
    public List<FlightLine> selectByCondition(@Param("starttime") String starttime, @Param("endtime") String endtime, @Param("city") String city);

    @Select("SELECT * FROM AIRPORT.FLIGHT WHERE BEGINTIME LIKE CONCAT('%', #{starttime}, '%') AND ENDPLACE LIKE CONCAT('%', #{endplace}, '%')AND BEGINPLACE LIKE CONCAT('%', #{beginplace}, '%')  AND ISPERMIT=1 and ISDELETE=0")
    public List<FlightLine> selectByCondition2(@Param("starttime") String starttime, @Param("endplace") String endplace,@Param("beginplace") String beginplace);


    @Select("SELECT * FROM AIRPORT.FLIGHT WHERE BEGINTIME BETWEEN #{starttime} AND #{endtime} AND ENDPLACE LIKE CONCAT('%', #{endplace}, '%')AND BEGINPLACE LIKE CONCAT('%', #{beginplace}, '%')  AND ISPERMIT=1 and ISDELETE=0")
    IPage<FlightLine> selectByConditionPage(Page<?> page,@Param("starttime") String starttime,@Param("endtime") String endtime, @Param("endplace") String endplace,@Param("beginplace") String beginplace);
    //根据开始时间和目的地查询航班信息
    @Select("SELECT * FROM AIRPORT.FLIGHT WHERE BEGINTIME LIKE CONCAT('%', #{starttime}, '%') AND ENDPLACE LIKE CONCAT('%', #{city}, '%') AND ISPERMIT=1 and ISDELETE=0")
    public IPage<FlightLine> selectByCon(Page<?> page,@Param("starttime") String starttime,  @Param("city") String city);

    List<Endplace> selectByEndplace();

    List<BeginTime> selectByBeginTime();
}
